var axios = require('axios');
let httpExtendMethod = require('./httpExtendMethod');

const isTestServe = false;

// 默认值
const defaultAxiosOption = {
  baseURL: '',
  timeout: 1000 * 60
};

const ECP_TOKEN_KEY = 'ecp_token';

let httpInstance = null;

// 拦截器类型
const interceptorsType = {
  request: 'request',
  response: 'response'
};

/**
 * 创建axios实例
 * @param { Object } axiosConfig
 */
function createAxios(axiosConfig) {
  let axiosOption = defaultAxiosOption;
  if (axiosConfig) {
    axiosOption = Object.assign({}, defaultAxiosOption, axiosConfig);
  }
  let baseURL = 'https://ygjy.ismartwork.cn';
  if (isTestServe) {
    baseURL = 'https://test/testing';
  }
  axiosOption.baseURL = baseURL + getRestfulUrl();
  const instance = axios.create(axiosOption);
  Object.setPrototypeOf(instance, axios);
  return instance;
}

/**
 * http实例自定义的方法
 */
function defineHttpCustomMethod() {
  if (!httpInstance) {
    return;
  }
  // 扩展http实例方法
  httpExtendMethod(httpInstance);
}

/**
 * 初始化http实例
 * @param { Object } axiosConfig
 */
function initHttpInstance(axiosConfig) {
  httpInstance = createAxios(axiosConfig);
  defineHttpCustomMethod(httpInstance);
}


/**
 * 获取http实例
 */
function getHttpInstance() {
  return httpInstance;
}

/**
 * 设置baseUrl
 * @param { String } baseURL
 * @returns
 */
function setBaseUrl(baseURL) {
  if (httpInstance == null) {
    return;
  }
  httpInstance.defaults.baseURL = baseURL;
}

/**
 * 获取baseUrl
 */
function getBaseUrl() {
  if (httpInstance == null) {
    return '';
  }
  return httpInstance.defaults.baseURL;
}

/**
 * 获取webSocketUrl
 */
function getWebsocketUrl() {
  let wsBaseUrl = getRawBaseUrl().replace(/^http/, '');
  if (isTestServe) {
    wsBaseUrl = '://10.51.111.9:8012';
  }
  return wsBaseUrl;
}

/**
 * 获取登录用户令牌
 */
function getLoginUserAccessToken() {
}

/**
 * 获取服务令牌
 */
function getAccessToken() {
  if (httpInstance == null) {
    return '';
  }
  return getLoginUserAccessToken();
}

/**
 * 设置登录的上下文到header
 * @param {*} config
 * @returns
 */
function setLoginTokenToConfig(config) {
  if (!config || !config.headers) {
    return;
  }
  // 必须通过getAccessToken统一获取，有可能是单点登录传入token直接取数，这时不能从本地全局用户缓存中获取
  const accessToken = getAccessToken();
  if (accessToken) {
    config.headers.common[ECP_TOKEN_KEY] = accessToken;
  }
}

/**
 * 清除旧的ECP_TOKEN
 */
function clearCommonECPToken() {
  httpInstance.defaults.headers.common[ECP_TOKEN_KEY] = null;
}


/**
 * 获取拦截器
 * @params { String } type
 */
function getInterceptors(type) {
  if (type === interceptorsType.request) {
    return httpInstance.interceptors.request;
  }
  if (type === interceptorsType.response) {
    return httpInstance.interceptors.response;
  }
  return null;
}

function getRestfulUrl() {
  return '/ecs/mapp/restful/';
}

function getImageBaseUrl() {
  return getBaseUrl() + 'filecommon/showPic/';
}

function getRawBaseUrl() {
  const restfulUrl = getRestfulUrl();
  return getBaseUrl().replace(restfulUrl, '');
}

/**
 * 获取移动端地址.
 * @param { relativeUrl } 相对地址
 */
function getMobileUrl(relativeUrl) {
  const token = Vue.loginService.getLoginUserInfo().accessToken;
  return `${getRawBaseUrl()}${relativeUrl}?local=true&accessToken=${token}`;
}

/**
 * 获取CancelToken
 */
function getCancelToken() {
  return axios.CancelToken;
}

initHttpInstance();

module.exports = {
  isTestServe,
  createHttpInstance: createAxios,
  getInterceptors,
  getHttpInstance,
  interceptorsType,
  setLoginTokenToConfig,
  setBaseUrl,
  getBaseUrl,
  getWebsocketUrl,
  getRestfulUrl,
  getImageBaseUrl,
  getAccessToken,
  getCancelToken,
  clearCommonECPToken,
  getMobileUrl,
  getRawBaseUrl
};
